<?php
$IGNORE_NUM = 0;
$importedfile = "";

if(isset($_POST["task"])) {
	if($_POST["task"] == "export") {
		header("Content-type: application/octet-stream");
		header("Content-Disposition: attachment; filename=\"file.bob\"");
		$filtered = str_replace('\\', '', $_POST["data"]);
		$data = json_decode($filtered);
		$layers = $data->layers;
		$spawnmat = $data->spawnIn;
		$spawn_underwater = $data->spawnUnderwater;
		$output = "";
		
		for($layer = 0; $layer < count($layers); $layer++) {
			$currLayer = $layers[$layer];
			for($col = 0; $col < count($currLayer); $col++) {
				for($row = 0; $row < count($currLayer[$col]); $row++) {
					$val = $currLayer[$col][$row];
					if($val != $IGNORE_NUM) {
						$output .= "$row,$col,$layer:$val\n";
					}
				}
			}
		}
		
		# add meta-information
		$output .= "METBEGIN\n";
		$output .= "$spawnmat\n$spawn_underwater\n";
		$output .= "dim:" . count($layers[0]) . "," . count($layers[0][0]);
		
		echo $output;
		# don't output any HTML in this case
		die;
	} else if($_POST["task"] == "import") {
		if($_FILES['bobfile']['tmp_name'] != "")
			$importedfile = file_get_contents($_FILES['bobfile']['tmp_name']);
	}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<!--
		BOB main page
		Author: Jason Ganzhorn
	-->

	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	
		<title>BOB</title>
		<link rel="stylesheet" type="text/css" href="styles/bob_page.css" />
		<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/prototype/1.7.0.0/prototype.js"></script>
		<script type="text/javascript" src="jscript/image_manager.js"></script>
		<script type="text/javascript" src="jscript/bob_manager.js"></script>
	</head>
	
	<body>
		<div id="layout">
			<fieldset>
				<legend>BOB Layout Designer</legend>
				<p>
					Layer Size: <input type="text" id="layer_size_x" value="15" /> x <input type="text" id="layer_size_y" value="15" />
					<button id="b_change_sz">Change Size</button>
					<button id="b_clear_layer">Clear Layer</button>
					<button id="b_clear_alllayers">Clear All</button>
					<strong>Changing the size clears ALL layers. Be careful.</strong>
				</p>
				<pre id="importfile"><?php echo $importedfile; ?></pre>
				<div id="height_selector"><button id="b_offset_down">&lt;</button><input type="text" id="layer_offset" value="0" /><button id="b_offset_up">&gt;</button></div>
				<div id="layout_tiles">
				</div>
			</fieldset>
		</div>
		
		<div id="controls">
			<fieldset id="layer_controls">
				<legend>Layer Control</legend>
				<p>Selected Layer <input type="text" id="layer_index" value="0" /><button id="b_layer_go">Go</button></p>
				<p><button id="layer_up">Up</button><button id="layer_down">Down</button></p>
			</fieldset>
			
			<fieldset id="extra_options">
				<legend>Extra Options</legend>
				<p><label><input id="underwater_spawn" type="checkbox"> Enable underwater spawning?</label></p>
				<p>Spawn in:<br />
					<select id="spawn_mat" size="3">
						<option value="3" selected="true">Dirt (default)</option>
						<option value="82">Clay</option>
						<option value="13">Gravel</option>
						<option value="79">Ice</option>
						<option value="12">Sand</option>
						<option value="78">Snow</option>
					</select>
				</p>
			</fieldset>
			
			<fieldset id="import_export">
				<legend>Import/Export</legend>
				<p><button class="fullbutton" id="b_file_export">Export File</button><button class="fullbutton" id="b_file_import">Import File</button></p>
				<p><form id="uploadbob" action="" method="post" enctype="multipart/form-data">File to import:<br /><input type="file" id="bobfile" name="bobfile" value="" /><input type="hidden" name="task" value="import" /></form></p>
			</fieldset>
			<fieldset id="helpful_links">
				<legend>Helpful Links</legend>
				<p>
					<a href="http://www.minecraftwiki.net/images/8/8c/DataValuesBeta.png" target="_blank">Quick Visual ID List</a><br />
					<a href="http://www.minecraftwiki.net/wiki/Block_ids" target="_blank">Block ID Wiki</a><br />
					<a href="http://code.google.com/p/web-bob/wiki/Usage" target="_blank">How to use Web-Bob</a>
				</p>
			</fieldset>
		</div>
	</body>
</html>
<?php
#}
?>
